package com.example.service;

import com.example.mapper.*;
import com.example.pojo.bo.JobSeekerBO;
import com.example.pojo.bo.LearningExperienceBO;
import com.example.pojo.bo.WorkExperienceBO;
import com.example.pojo.dto.CollectDTO;
import com.example.pojo.dto.UserInfoDTO;
import com.example.pojo.entity.Collect;
import com.example.pojo.entity.JobRecruiter;
import com.example.pojo.entity.Post;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 本文来演示
 *
 * @author 赵建云
 * @create 2023/10/10
 */
@Service
public class CollectServiceImpl implements CollectService{

    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private JobRecruiterMapper jobRecruiterMapper;
    @Autowired
    private JobSeekerMapper jobSeekerMapper;
    @Autowired
    private LearningExperienceMapper learningExperienceMapper;
    @Autowired
    private WorkExperienceMapper workExperienceMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private DriverMapper driverMapper;
    @Override
    public int collectSave(Integer jsId, Integer posId) {
        int count = collectMapper.collectSave(jsId,posId);
        return count;
    }

    @Override
    public int collectDelete(Integer jsId, Integer posId) {
        int count = collectMapper.collectDelete(jsId,posId);
        return count;
    }

    @Override
    public List<CollectDTO> findTotal(Integer JsId) {
        List<CollectDTO> list = new ArrayList<>();
        List<Integer> posIds = collectMapper.collectByJsId(JsId);
        for (Integer posId : posIds) {
            Post post = postMapper.findAllByPostId(posId);
            CollectDTO collectDTO = new CollectDTO();
            collectDTO.setPosId(post.getId());
            collectDTO.setPosName(post.getPosName());
            collectDTO.setExpDemand(post.getExpDemand());
            collectDTO.setRecruitsNum(post.getRecruitsNum());
            collectDTO.setWorkPlace(post.getWorkPlace());
            Integer jrId = post.getJrId();
            collectDTO.setJrId(jrId);
            JobRecruiter jobRecruiter = jobRecruiterMapper.fingJobRecruiterById(jrId);
            collectDTO.setCompanyName(jobRecruiter.getCompanyName());
            collectDTO.setJrAvatar(jobRecruiter.getJrAvatar());
            collectDTO.setContactPerson(jobRecruiter.getContactPerson());
            if (post.getBackground().equals("0")){
                collectDTO.setBackground("不限");
            }else if (post.getBackground().equals("1")){
                collectDTO.setBackground("本科");
            }else if (post.getBackground().equals("2")){
                collectDTO.setBackground("硕士");
            }else {
                collectDTO.setBackground("博士");
            }
            if (null!=post.getPayFloor()&&post.getPayFloor()>=1000){
                collectDTO.setPay(post.getPayFloor()/1000+"k"+"-"+post.getPayUpper()/1000+"k");
            }else {
                collectDTO.setPay(post.getPayFloor()+"元"+"-"+post.getPayUpper()+"元");
            }
            collectDTO.setCompanyType(jobRecruiter.getCompanyType());
            list.add(collectDTO);
        }
        return list;
    }

    @Override
    public UserInfoDTO findUserInfo(Integer jsId) {
        System.out.println("=findUserInfo=");
        System.out.println(jsId);
        JobSeekerBO jobSeekerBO = jobSeekerMapper.searchByWord(jsId);
        LearningExperienceBO learningExperienceBO = learningExperienceMapper.searchAll(jsId);
        WorkExperienceBO workExperienceBO = workExperienceMapper.search(jsId);
        System.out.println("=CollectServiceImpl=");
        System.out.println(workExperienceBO);
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        userInfoDTO.setJsName(jobSeekerBO.getJsName());
        userInfoDTO.setAvatar(jobSeekerBO.getAvatar());
        userInfoDTO.setJsSex(jobSeekerBO.getJsSex());
        if (jobSeekerBO.getAge()!=null){
            userInfoDTO.setAge(jobSeekerBO.getAge()+"岁");
        }

        if (workExperienceBO!=null) {
            if (workExperienceBO!=null&&workExperienceBO.getExperience()==0){
                userInfoDTO.setExperience("应届生");
            }else {
                userInfoDTO.setExperience("已工作"+workExperienceBO.getExperience()+"年");
            }
        }
        if (learningExperienceBO!=null) {
            if (learningExperienceBO.getBackground()==1){
                userInfoDTO.setBackground("本科");
            }else if (learningExperienceBO.getBackground()==2){
                userInfoDTO.setBackground("硕士");
            }else {
                userInfoDTO.setBackground("博士");
            }
        }
        return userInfoDTO;
    }

    @Override
    public PageInfo<CollectDTO> findById(Integer pageNum, Integer pageSize, Integer jsId) {
        PageInfo<CollectDTO> pageInfo = null;
        Page<CollectDTO> page = PageHelper.startPage(pageNum,pageSize);
        List<CollectDTO> list = findTotal(jsId);
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Collect searchCollect(Integer jsId, Integer posId) {
        Collect collect = collectMapper.searchCollect(jsId,posId);
        return collect;
    }

    @Override
    public List<CollectDTO> messageBySid(Integer jsId) {
        List<CollectDTO> list = new ArrayList<>();
        List<Integer> posIds = messageMapper.messageById(jsId);
        for (Integer posId : posIds) {
            Post post = postMapper.findAllByPostId(posId);
            CollectDTO collectDTO = new CollectDTO();
            collectDTO.setPosId(post.getId());
            collectDTO.setPosName(post.getPosName());
            collectDTO.setExpDemand(post.getExpDemand());
            collectDTO.setRecruitsNum(post.getRecruitsNum());
            collectDTO.setWorkPlace(post.getWorkPlace());
            Integer jrId = post.getJrId();
            collectDTO.setJrId(jrId);
            JobRecruiter jobRecruiter = jobRecruiterMapper.fingJobRecruiterById(jrId);
            collectDTO.setCompanyName(jobRecruiter.getCompanyName());
            collectDTO.setJrAvatar(jobRecruiter.getJrAvatar());
            collectDTO.setContactPerson(jobRecruiter.getContactPerson());
            if (post.getBackground().equals("0")){
                collectDTO.setBackground("不限");
            }else if (post.getBackground().equals("1")){
                collectDTO.setBackground("本科");
            }else if (post.getBackground().equals("2")){
                collectDTO.setBackground("硕士");
            }else {
                collectDTO.setBackground("博士");
            }
            if (null!=post.getPayFloor()&&post.getPayFloor()>=1000){
                collectDTO.setPay(post.getPayFloor()/1000+"k"+"-"+post.getPayUpper()/1000+"k");
            }else {
                collectDTO.setPay(post.getPayFloor()+"元"+"-"+post.getPayUpper()+"元");
            }
            collectDTO.setCompanyType(jobRecruiter.getCompanyType());
            list.add(collectDTO);
        }
        return list;
    }

    @Override
    public PageInfo<CollectDTO> messageFindBySid(Integer pageNum, Integer pageSize, Integer jsId) {
        PageInfo<CollectDTO> pageInfo = null;
        Page<CollectDTO> page = PageHelper.startPage(pageNum,pageSize);
        List<CollectDTO> list = messageBySid(jsId);
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<CollectDTO> submitByJsId(Integer jsId) {
        List<CollectDTO> list = new ArrayList<>();
        List<Integer> posIds = driverMapper.driverSubmitByJsId(jsId);
        for (Integer posId : posIds) {
            Post post = postMapper.findAllByPostId(posId);
            CollectDTO collectDTO = new CollectDTO();
            collectDTO.setPosId(post.getId());
            collectDTO.setPosName(post.getPosName());
            collectDTO.setExpDemand(post.getExpDemand());
            collectDTO.setRecruitsNum(post.getRecruitsNum());
            collectDTO.setWorkPlace(post.getWorkPlace());
            Integer jrId = post.getJrId();
            collectDTO.setJrId(jrId);
            JobRecruiter jobRecruiter = jobRecruiterMapper.fingJobRecruiterById(jrId);
            collectDTO.setCompanyName(jobRecruiter.getCompanyName());
            collectDTO.setJrAvatar(jobRecruiter.getJrAvatar());
            collectDTO.setContactPerson(jobRecruiter.getContactPerson());
            if (post.getBackground().equals("0")){
                collectDTO.setBackground("不限");
            }else if (post.getBackground().equals("1")){
                collectDTO.setBackground("本科");
            }else if (post.getBackground().equals("2")){
                collectDTO.setBackground("硕士");
            }else {
                collectDTO.setBackground("博士");
            }
            if (null!=post.getPayFloor()&&post.getPayFloor()>=1000){
                collectDTO.setPay(post.getPayFloor()/1000+"k"+"-"+post.getPayUpper()/1000+"k");
            }else {
                collectDTO.setPay(post.getPayFloor()+"元"+"-"+post.getPayUpper()+"元");
            }
            collectDTO.setCompanyType(jobRecruiter.getCompanyType());
            list.add(collectDTO);
        }
        return list;
    }

    @Override
    public PageInfo<CollectDTO> submitFindBySid(Integer pageNum, Integer pageSize, Integer jsId) {
        PageInfo<CollectDTO> pageInfo = null;
        Page<CollectDTO> page = PageHelper.startPage(pageNum,pageSize);
        List<CollectDTO> list = submitByJsId(jsId);
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<CollectDTO> interviewByJsId(Integer jsId) {
        List<CollectDTO> list = new ArrayList<>();
        List<Integer> posIds = driverMapper.driverInterviewByJsId(jsId);
        for (Integer posId : posIds) {
            Post post = postMapper.findAllByPostId(posId);
            CollectDTO collectDTO = new CollectDTO();
            collectDTO.setPosId(post.getId());
            collectDTO.setPosName(post.getPosName());
            collectDTO.setExpDemand(post.getExpDemand());
            collectDTO.setRecruitsNum(post.getRecruitsNum());
            collectDTO.setWorkPlace(post.getWorkPlace());
            Integer jrId = post.getJrId();
            collectDTO.setJrId(jrId);
            JobRecruiter jobRecruiter = jobRecruiterMapper.fingJobRecruiterById(jrId);
            collectDTO.setCompanyName(jobRecruiter.getCompanyName());
            collectDTO.setJrAvatar(jobRecruiter.getJrAvatar());
            collectDTO.setContactPerson(jobRecruiter.getContactPerson());
            if (post.getBackground().equals("0")){
                collectDTO.setBackground("不限");
            }else if (post.getBackground().equals("1")){
                collectDTO.setBackground("本科");
            }else if (post.getBackground().equals("2")){
                collectDTO.setBackground("硕士");
            }else {
                collectDTO.setBackground("博士");
            }
            if (null!=post.getPayFloor()&&post.getPayFloor()>=1000){
                collectDTO.setPay(post.getPayFloor()/1000+"k"+"-"+post.getPayUpper()/1000+"k");
            }else {
                collectDTO.setPay(post.getPayFloor()+"元"+"-"+post.getPayUpper()+"元");
            }
            collectDTO.setCompanyType(jobRecruiter.getCompanyType());
            list.add(collectDTO);
        }
        return list;
    }

    @Override
    public PageInfo<CollectDTO> interviewFindBySid(Integer pageNum, Integer pageSize, Integer jsId) {
        PageInfo<CollectDTO> pageInfo = null;
        Page<CollectDTO> page = PageHelper.startPage(pageNum,pageSize);
        List<CollectDTO> list = interviewByJsId(jsId);
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
